package com.swdegao.iot.netty.server;

import com.swdegao.iot.netty.entity.Ports;
import com.swdegao.iot.netty.server.dongjianhandler.DongJianChannelInitializer;
import com.swdegao.iot.netty.server.guoxinhandler.GuoXinChannelInitializer;
import com.swdegao.iot.netty.server.qianbaotonghandler.QianBaoTong_NettyServerChannelInitializer;
import com.swdegao.iot.netty.server.qianbaotongplushandler.QianBaoTongPlusChannelInitializer;
import com.swdegao.iot.netty.server.silianhandler.SiLian_NettyServerChannelInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class NettyServer {
	// 定义一个全局的记录器，通过LoggerFactory获取
	private final static Logger logger = LoggerFactory.getLogger(NettyServer.class);

    private void start(Ports port){
        try {
            //德高大表TCP
            if(null!=port.getDegao_Terminals_port() && port.getDegao_Terminals_port()>0){
                deGaoChannelInit(port.getDegao_Terminals_port(),1,2);
            }

            //国信小表集中器TCP
            if(null!=port.getGuoxin_tcp_port() && port.getGuoxin_tcp_port()>0){
                guoXinChannelInit(port.getGuoxin_tcp_port(),1,8);
            }

            //四联小表集中器TCP
            if(null!=port.getSilian_tcp_port() && port.getSilian_tcp_port()>0){
                siLian_ChannelInit(port.getSilian_tcp_port(),1,2);
            }

            //千宝通小表集中器TCP
            if(null!=port.getQianbaotong_tcp_port() && port.getQianbaotong_tcp_port()>0){
                qianbaotong_ChannelInit(port.getQianbaotong_tcp_port(),1,2);
            }

            //东健大表TCP
            if(null!=port.getDongjian_tcp_port() && port.getDongjian_tcp_port()>0){
                dongJianChannelInit(port.getDongjian_tcp_port(),1,2);
            }
        }catch (Exception ex){
            logger.info("小表 Netty 启动异常:" + ex.getMessage());
        }

    }


    //四联集中器tcp
    private void siLian_ChannelInit(Integer port,Integer bossGroupCount,Integer workerGroupCount) throws InterruptedException {
        EventLoopGroup bossGroup  = new NioEventLoopGroup(bossGroupCount);
        EventLoopGroup workerGroup  = new NioEventLoopGroup(workerGroupCount);
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new SiLian_NettyServerChannelInitializer());
            b.option(ChannelOption.SO_BACKLOG,128);
            b.childOption(ChannelOption.SO_KEEPALIVE, true);
            b.childOption(ChannelOption.TCP_NODELAY, true);
            b.childOption(ChannelOption.SO_REUSEADDR, true);
            ChannelFuture f = b.bind(port).sync();
            logger.info("开始监听端口：{}",port);
        }catch (Exception ex){
            logger.info("四联集中器 Netty 端口 启动错误:" + ex.getMessage());
            bossGroup.shutdownGracefully().sync();
            workerGroup.shutdownGracefully().sync();
        }
    }

    //千宝通集中器tcp
    private void qianbaotong_ChannelInit(Integer port,Integer bossGroupCount,Integer workerGroupCount) throws InterruptedException {
        EventLoopGroup bossGroup  = new NioEventLoopGroup(bossGroupCount);
        EventLoopGroup workerGroup  = new NioEventLoopGroup(workerGroupCount);
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new QianBaoTongPlusChannelInitializer());
            b.option(ChannelOption.SO_BACKLOG,128);
            b.childOption(ChannelOption.SO_KEEPALIVE, true);
            b.childOption(ChannelOption.TCP_NODELAY, true);
            b.childOption(ChannelOption.SO_REUSEADDR, true);
            ChannelFuture f = b.bind(port).sync();
            logger.info("开始监听千宝通端口：{}",port);
        }catch (Exception ex){
            logger.info("千宝通集中器 Netty 端口 启动错误:" + ex.getMessage());
            bossGroup.shutdownGracefully().sync();
            workerGroup.shutdownGracefully().sync();
        }
    }



    //德高大表tcp- 端口8211
    private void deGaoChannelInit(Integer port,Integer bossGroupCount,Integer workerGroupCount) throws InterruptedException {
        EventLoopGroup bossGroup  = new NioEventLoopGroup(bossGroupCount);
        EventLoopGroup workerGroup  = new NioEventLoopGroup(workerGroupCount);
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new NettyServerChannelInitializer());
            b.option(ChannelOption.SO_BACKLOG,128);
            b.childOption(ChannelOption.SO_KEEPALIVE, true);
            b.childOption(ChannelOption.TCP_NODELAY, true);
            b.childOption(ChannelOption.SO_REUSEADDR, true);
            ChannelFuture f = b.bind(port).sync();
            logger.info("开始监听端口：{}",port);
//            f.channel();
        }catch (Exception ex){
            logger.info("德高大表 Netty 端口 启动错误:" + ex.getMessage());
            bossGroup.shutdownGracefully().sync();
            workerGroup.shutdownGracefully().sync();
        }
    }



    //国信tcp- 端口8211
    private void guoXinChannelInit(Integer port,Integer bossGroupCount,Integer workerGroupCount) throws InterruptedException {
        EventLoopGroup bossGroup  = new NioEventLoopGroup(bossGroupCount);
        EventLoopGroup workerGroup  = new NioEventLoopGroup(workerGroupCount);
        try {
            ServerBootstrap b2 = new ServerBootstrap();
            b2.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new GuoXinChannelInitializer());
            b2.option(ChannelOption.SO_BACKLOG,128);
            b2.childOption(ChannelOption.SO_KEEPALIVE, true);
            b2.childOption(ChannelOption.TCP_NODELAY, true);
            b2.childOption(ChannelOption.SO_REUSEADDR, true);
            ChannelFuture f = b2.bind(port).sync();
            logger.info("开始监听端口：{}",port);
//            f.channel();
        }catch (Exception ex){
            logger.info("德高大表 Netty 端口 启动错误:" + ex.getMessage());
            bossGroup.shutdownGracefully().sync();
            workerGroup.shutdownGracefully().sync();
        }
    }

    //东健tcp- 端口9401
    private void dongJianChannelInit(Integer port,Integer bossGroupCount,Integer workerGroupCount) throws InterruptedException {
        EventLoopGroup bossGroup  = new NioEventLoopGroup(bossGroupCount);
        EventLoopGroup workerGroup  = new NioEventLoopGroup(workerGroupCount);
        try {
            ServerBootstrap b2 = new ServerBootstrap();
            b2.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new DongJianChannelInitializer());
            b2.option(ChannelOption.SO_BACKLOG,128);
            b2.childOption(ChannelOption.SO_KEEPALIVE, true);
            b2.childOption(ChannelOption.TCP_NODELAY, true);
            b2.childOption(ChannelOption.SO_REUSEADDR, true);
            ChannelFuture f = b2.bind(port).sync();
            logger.info("开始监听端口：{}",port);
//            f.channel();
        }catch (Exception ex){
            logger.info("德高大表 Netty 端口 启动错误:" + ex.getMessage());
            bossGroup.shutdownGracefully().sync();
            workerGroup.shutdownGracefully().sync();
        }
    }

    public void doStart(Ports port){
        try {
            start(port);
        } catch (Exception e) {
            logger.info("Netty 启动错误: " + e.getMessage());
        }
    }


}
